<ui:composition xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:p="http://primefaces.org/ui"
	template="/templates/masterPage.xhtml">
	<ui:define name="pageHeader">
		<div>
			<h1>Admin Page</h1>
		</div>
	</ui:define>

	<ui:define name="content">
		<div class="panel">
			<h:form id="addform">
				<p:panel id="panel" header="New User">
					<h:panelGrid columns="2">
						<h:outputLabel for="login" value="Login: *" />
						<p:inputText id="login" value="#{usersBean.login}" required="true"
							label="Login">
							<f:validateLength minimum="4" />
							<f:converter converterId="userInputConverter" />
						</p:inputText>

						<h:outputLabel for="firstName" value="First Name: *" />
						<p:inputText id="firstName" value="#{usersBean.firstName}"
							required="true" label="First Name">
							<f:converter converterId="userInputConverter" />
						</p:inputText>

						<h:outputLabel for="lastName" value="Last Name: *" />
						<p:inputText id="lastName" value="#{usersBean.lastName}"
							required="true" label="Last Name">
							<f:converter converterId="userInputConverter" />
						</p:inputText>

						<h:outputLabel for="email" value="Email: *" />
						<p:inputText id="email" value="#{usersBean.email}" required="true"
							label="Email">
							<f:converter converterId="userInputConverter" />
							<f:validator validatorId="emailValidator" />
						</p:inputText>
							
						<h:outputLabel for="role" value="Role: *" />
						<p:selectOneMenu id="role" value="#{usersBean.role}" required="true">  
			                <f:selectItem itemLabel="Assign role..." itemValue="" />  
			                <f:selectItems value="#{usersBean.getRoles()}" />  
			            </p:selectOneMenu>

						<h:panelGroup />
						<p:commandButton value="Add User" actionListener="#{usersBean.saveUser}" update=":feedback, :form:userTable, :addform">
						</p:commandButton>
					</h:panelGrid>
				</p:panel>
			</h:form>
		</div>
		<div class="panel">
			<h:form id="form">
				<p:panel id="panel" header="List of users">
					<p:dataTable var="user" value="#{lazyUsersDataModel}"
						paginator="true" rows="10"
						paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
						rowsPerPageTemplate="5,10,15" id="userTable" lazy="true" styleClass="table-users">

						<p:column headerText="Login" sortBy="login"
							filterBy="#{user.login}">
							<h:outputText value="#{user.login}" />
						</p:column>

						<p:column headerText="First Name" sortBy="firstName"
							filterBy="#{user.firstName}">
							<h:outputText value="#{user.firstName}" />
						</p:column>

						<p:column headerText="Last Name" sortBy="lastName"
							filterBy="#{user.lastName}">
							<h:outputText value="#{user.lastName}" />
						</p:column>

						<p:column headerText="Role" sortBy="role" filterBy="#{user.role}">
							<h:outputText value="#{user.role}" />
						</p:column>

						<p:column headerText="E-mail" sortBy="email"
							filterBy="#{user.email}">
							<h:outputText value="#{user.email}" />
						</p:column>

						<p:column>
							<p:commandButton value="Delete"
								rendered="#{not request.remoteUser.equals(user.login)}"
								update=":form:userTable"
								icon="ui-icon-circle-close"
								actionListener="#{usersBean.deleteUser(user.id)}">
								<p:confirm header="Confirmation" message="Are you sure?"
									icon="ui-icon-alert" />
							</p:commandButton>

							<p:confirmDialog global="true" showEffect="fade"
								hideEffect="explode">
								<p:commandButton value="Yes" type="button"
									styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
								<p:commandButton value="No" type="button"
									styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
							</p:confirmDialog>
						</p:column>

					</p:dataTable>
				</p:panel>
			</h:form>
			<ui:insert name="feedback">
				<ui:include src="/templates/feedback.xhtml" />
			</ui:insert>
		</div>
	</ui:define>

	<ui:define name="footer">
		<div />
	</ui:define>
</ui:composition>
